﻿package com.projects.xenia.prompts_activities;

import java.util.ArrayList;
import java.util.UUID;

import com.projects.xenia.R;
import com.projects.xenia.R.id;
import com.projects.xenia.R.layout;
import com.projects.xenia.documents_classes.Documents_Order;
import com.projects.xenia.documents_classes.Documents_Order.DocLineAdd;
import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Resources.Theme;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.FilterQueryProvider;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class Activity_OrderGoodsDetails extends Activity {
		
	private Documents_Order DocumentClass;
	
	private ArrayList <OrderGoodsDetailsItem> OrderGoodsDetailsItemCollection = new ArrayList <OrderGoodsDetailsItem> ();	
	
	private ImageView OrderGoodsDetailsBackWard;
	
	private ListView OrderGoodsDetailsItemsListView;
	private ArrayAdapter <OrderGoodsDetailsItem> adap;
	
	private class OrderGoodsDetailsItem {
		
		public String id;
		public String descr;
		
		@Override
		public String toString() {
			return this.descr;
		}
		
	}
	
	private Cursor GetOrderGoodsDetailsItems () {
		
		Cursor OrderGoodsDetailsItemsCursor = MainGlobalModule.DBE.rawQuery("select id as _id, descr from xref_OrdersGoodsDetails", null);
		if (OrderGoodsDetailsItemsCursor!=null && OrderGoodsDetailsItemsCursor.getCount() > 0){
			OrderGoodsDetailsItemsCursor.moveToFirst();				
			while (OrderGoodsDetailsItemsCursor.isAfterLast()==false) {
				OrderGoodsDetailsItem goi = new OrderGoodsDetailsItem ();
				goi.id = OrderGoodsDetailsItemsCursor.getString(0);
				goi.descr = OrderGoodsDetailsItemsCursor.getString(1);
				OrderGoodsDetailsItemCollection.add(goi);
				OrderGoodsDetailsItemsCursor.moveToNext();
			}
			OrderGoodsDetailsItemsCursor.close();
		}
		
		return null;		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);		
	    setContentView(R.layout.order_goodsdetails);			
		
	    
	    OrderGoodsDetailsBackWard = (ImageView)findViewById(R.id._backward);
		OrderGoodsDetailsBackWard.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				SaveAndClose ();
			}
		});		
		
		GetOrderGoodsDetailsItems ();	    
			
		OrderGoodsDetailsItemsListView = (ListView)findViewById(R.id.OrderGoodsDetailsItemsListView);
		OrderGoodsDetailsItemsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);		
		
		adap = new ArrayAdapter <OrderGoodsDetailsItem> (this, android.R.layout.simple_list_item_multiple_choice , OrderGoodsDetailsItemCollection);		
		OrderGoodsDetailsItemsListView.setAdapter(adap);
		OrderGoodsDetailsItemsListView.setOnItemClickListener(OrderGoodsDetailsItemsListView_Select);
		
	    DocumentClass = (Documents_Order)MainGlobalModule.DocumentClass;
	    SetChoiceByDoc ();
	    
	}
	

	private OnItemClickListener OrderGoodsDetailsItemsListView_Select = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {		
			
			OrderGoodsDetailsItem ogd = (OrderGoodsDetailsItem)arg0.getItemAtPosition(position);			
			DocumentClass.insertUpdateLineAddByKey (ogd.id,ogd.descr,!((CheckedTextView) arg1).isChecked());	
			
		}	
	};
	
	

	private void SetChoiceByDoc () {
		
		int i = 0;
		for (OrderGoodsDetailsItem ogd : OrderGoodsDetailsItemCollection){			
			if (!(DocumentClass.DocLineAddCollectionFind (ogd.id)==null)) {
				OrderGoodsDetailsItemsListView.setItemChecked(i, true);
			}			
			i++;
		}
		
	}
	
	private void SaveAndClose () {		
		
		finish();
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {		
		
		switch (keyCode){		
    		case KeyEvent.KEYCODE_BACK:    			
    			SaveAndClose();
    			return false;
    	}	
		
		return super.onKeyDown(keyCode, event);
	}
	
}
